package com.richapp.launcher.scene.actions;

import com.alibaba.fastjson.JSONObject;
import com.richapp.launcher.database.system.dao.BoxModelDao;
import com.richapp.launcher.database.system.mode.BoxModel;
import com.richapp.launcher.database.system.mode.DeviceModel;
import com.richapp.launcher.net.box.tcp.server.BoxServer;
import com.richapp.launcher.net.message.MsgConstant;
import com.richapp.launcher.net.mobile.server.UserServer;
import com.richapp.launcher.net.remote.RemoteClient;
import com.richapp.launcher.scene.SceneServer;
import com.richapp.launcher.scene.abstracts.SceneAbstractAction;
import com.richapp.launcher.serialport.gateway.command.ControlDeviceCmd;
import com.richapp.launcher.utils.Toolkit;
import com.richapp.launcher.utils.Tools;
import com.tencent.mid.util.Util;

public class SceneDimmingAction extends SceneAbstractAction{
	
	public SceneDimmingAction() {
	}

	@Override
	public int getActionType() {
		return SCENE_ACTION_TYPE_DIMMING;
	}

	@Override
	public void doAction() {
		DeviceModel device = getDevice();
		if(device != null){
			BoxModel boxModel = new BoxModelDao(SceneServer.getInstance().getContext()).getCurrentBox();
			if(boxModel.getBoxCpuId().equals(device.getBoxCpuId())){
//				if(!device.getDeviceStatus().equals(getDimmingValue())){
//					ControlDeviceCmd.getInstance().setDimming(device.getBoxCpuId(), device.getDeviceId(), device.getDeviceNumber(),getDimmingValue(),device.getWay());
//				}
				if (!Toolkit.isEmpty(getDimmingValue())) {
					ControlDeviceCmd.getInstance().setDimming(device.getBoxCpuId(), device.getDeviceId(), device.getDeviceNumber(), getDimmingValue(), device.getWay());
				}
			}else{
				//构造同步信息
				JSONObject obj = new JSONObject();
				obj.put("type", 5);
				obj.put("userName", boxModel.getUserName());
				obj.put("currentBoxCpuId", boxModel.getBoxCpuId());
				obj.put("boxCpuId", device.getBoxCpuId());
				obj.put("deviceId", device.getDeviceId());
				obj.put("deviceNumber", device.getDeviceNumber());
				obj.put("deviceStatus", getDimmingValue());

				//发送所有当前手机
				UserServer.getInstance().sendAllUsersMessage(obj.toJSONString());

				obj.put(MsgConstant.HEAD_IS_REPLY,"true");
				//发送服务器
				RemoteClient.getInstance().sendMessage(obj.toJSONString());
				//发送其他机顶盒
				BoxServer.getInstance().sendAllBoxMessage(obj.toJSONString());
			}
		}
	}
}
